#include <iostream>
#include <sstream>

#include <log.h>

int main()
{
    std::cout << std::boolalpha;

    OUTER_LOGGER() << "std::stringstream可以转化任意基础类型为基础类型";
    OUTER_LOGGER() << "举个例子: std::string -> int";
    CODE_LOGGER(int a = -1;);
    CODE_LOGGER(std::stringstream ss;);
    CODE_LOGGER(ss << "123";);
    CODE_LOGGER(ss >> a;);
    CODE_LOGGER(std::cout << "a: " << a << ", ss.fail: " << ss.fail() << std::endl;);

    OUTER_LOGGER() << "但是如果是非法的int就会让ss.fail()返回true";
    CODE_LOGGER(std::stringstream ss2;);
    CODE_LOGGER(ss2 << "abc";);
    CODE_LOGGER(ss2 >> a;);
    CODE_LOGGER(std::cout << "a: " << a << ", ss2.fail: " << ss2.fail() << std::endl;);

    HORIZONTAL_LINE();

    OUTER_LOGGER() << "std::stringstream解析一遍后不能再解析了";
    CODE_LOGGER(std::stringstream ss3;);
    CODE_LOGGER(ss3 << 1;);
    CODE_LOGGER(ss3 >> a;);
    CODE_LOGGER(std::cout << "a: " << a << ", ss3.fail: " << ss3.fail() << std::endl;);
    CODE_LOGGER(ss3 << 2;);
    CODE_LOGGER(ss3 >> a;);
    CODE_LOGGER(std::cout << "a: " << a << ", ss3.fail: " << ss3.fail() << std::endl;);

    OUTER_LOGGER() << "但是只需要clear就可以了";
    CODE_LOGGER(std::stringstream ss4;);
    CODE_LOGGER(ss4 << 1;);
    CODE_LOGGER(ss4 >> a;);
    CODE_LOGGER(std::cout << "a: " << a << ", ss4.fail: " << ss4.fail() << std::endl;);
    CODE_LOGGER(ss4.clear(););
    CODE_LOGGER(ss4 << 2;);
    CODE_LOGGER(ss4 >> a;);
    CODE_LOGGER(std::cout << "a: " << a << ", ss4.fail: " << ss4.fail() << std::endl;);

    HORIZONTAL_LINE();

    OUTER_LOGGER() << "std::stringstream的eof函数可以用来检查是否完全转化";
    CODE_LOGGER(std::stringstream ss5("1"););
    CODE_LOGGER(ss5 >> a;);
    CODE_LOGGER(std::cout << "bool: " << (bool)ss5 << ", eof: " << ss5.eof() << std::endl;);
    CODE_LOGGER(std::stringstream ss6("1a"););
    CODE_LOGGER(ss6 >> a;);
    CODE_LOGGER(std::cout << "bool: " << (bool)ss6 << ", eof: " << ss6.eof() << std::endl;);
    return 0;
}
